package com.sx.pipeline.ui.task.evaluate

import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hjq.widget.view.SimpleRatingBar
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActTaskEvaluateBinding
import com.sx.pipeline.databinding.ItemReleaseTaskEvaluateBinding
import com.sx.pipeline.repos.http.req.TaskEvaluateDTO
import com.sx.pipeline.repos.http.res.TakePartBean
import com.sx.pipeline.repos.http.res.TaskDetailVo
import com.sx.pipeline.ui.task.ReleaseTaskVM
import com.sx.pipeline.view.linearlayout.AdapterLinearLayout
import com.sx.pipeline.view.linearlayout.LinearLayoutAdapter


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 我的发布-任务列表
 */
@Route(path = RoutePath.Mine.TASK_EVALUATE)
class TaskEvaluateActivity : CYDataBindingBaseActivity<ActTaskEvaluateBinding, ReleaseTaskVM>(R.layout.act_task_evaluate),View.OnClickListener  {
    @JvmField
    @Autowired(name = IntentKey.DATA)
    var data: String? = ""

    private val idList: MutableList<Int> = ArrayList()

    private val unitIdList: MutableList<Int> = ArrayList()

    private val orderIdList: MutableList<Int> = ArrayList()

    private val starList: MutableList<Int> = ArrayList()

    private val contentList: MutableList<String> = ArrayList()

    var proDetail: TaskDetailVo ?=null

    private var taskEvaluateAddList: MutableList<TaskEvaluateDTO> = ArrayList()
    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
         proDetail = Gson().fromJson(data, object : TypeToken<TaskDetailVo?>() {}.type)
        if (proDetail?.takePartList?.size != 0) {
            initProducts()
            sortViewItem()
        }
    }


    private val productAdapter =
        object : LinearLayoutAdapter<TakePartBean>() {
            override fun getView(
                parent: AdapterLinearLayout<TakePartBean>,
                position: Int,
                data: TakePartBean
            ): View {
                val binding = ItemReleaseTaskEvaluateBinding.inflate(layoutInflater, parent, false)
                binding.sbUnitName.rightText=data.unitName
                setClickListener(binding, data, position)

                return binding.root
            }

            private fun setClickListener(
                binding: ItemReleaseTaskEvaluateBinding,
                data: TakePartBean,
                position: Int
            ) {

            }

        }


    private fun initProducts() {
        val proDetail: TaskDetailVo =
            Gson().fromJson(data, object : TypeToken<TaskDetailVo?>() {}.type)
        mBinding.llAddEvalution.adapter = productAdapter
        mBinding.llAddEvalution.onItemClickListener =
            object :
                AdapterLinearLayout.OnItemClickListener<TakePartBean> {
                override fun onItemClick(
                    parent: AdapterLinearLayout<TakePartBean>,
                    position: Int,
                    data: TakePartBean,
                    itemView: View
                ) {

                }
            }
        productAdapter.setData(proDetail.takePartList)
    }


    /**
     * 该方法主要用于排序（每个item中的序号），主要针对从中间删除item的情况
     */
    private fun sortViewItem() {
        for (i in 0 until mBinding.llAddEvalution.getChildCount()) {
            val viewItem: View = mBinding.llAddEvalution.getChildAt(i)
        }
    }

    private fun getContontList(): MutableList<String>? {
        for (i in 0 until mBinding.llAddEvalution.getChildCount()) {
            val itemView: View = mBinding.llAddEvalution.getChildAt(i)
            val etContent =
                itemView.findViewById<View>(R.id.et_content) as EditText
            if (etContent != null) {
                val vipNum = etContent.text.toString().trim { it <= ' ' }
                if (!TextUtils.isEmpty(vipNum)) {
                    contentList.add(vipNum)
                }
            }
        }
        return contentList
    }

    private fun getStarList(): MutableList<Int>? {
        for (i in 0 until mBinding.llAddEvalution.getChildCount()) {
            val itemView: View = mBinding.llAddEvalution.getChildAt(i)
            val etPrice =
                itemView.findViewById<View>(R.id.rc_rating_bar) as SimpleRatingBar
            if (etPrice != null) {
                val starCount = etPrice.grade
                if (!TextUtils.isEmpty(starCount.toString()
                    )) {
                    starList.add(starCount.toInt())
                }
            }
        }
        return starList
    }

    private fun getId(): MutableList<Int>? {
        for (i in 0 until proDetail?.takePartList?.size!!) {
            idList.add(proDetail?.takePartList!![i].id)
        }
        return idList
    }

    private fun getunitId(): MutableList<Int>? {
        for (i in 0 until proDetail?.takePartList?.size!!) {
            unitIdList.add(proDetail?.takePartList!![i].unitId)
        }
        return unitIdList
    }

    private fun getOrderId(): MutableList<Int>? {
        for (i in 0 until proDetail?.takePartList?.size!!) {
            orderIdList.add(proDetail?.takePartList!![i].orderId)
        }
        return orderIdList
    }



    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_submit->{
                getStarList()
                getContontList()
                getId()
                getunitId()
                getOrderId()
                if (contentList!!.size != mBinding.llAddEvalution.childCount) {
                    Toast.makeText(this, "请输入评价内容", Toast.LENGTH_SHORT).show()
                    idList.clear()
                    unitIdList.clear()
                    orderIdList.clear()
                    starList.clear()
                    contentList.clear()
                    return
                }
                var count = contentList.size;
                var i = 0;
                while (i < count) {
                    val id=idList.get(i)
                    val unitId=unitIdList.get(i)
                    val orderId=orderIdList.get(i)
                    val star=starList.get(i);
                    val content=contentList.get(i);
                    val jsonEvaluateAdd=TaskEvaluateDTO(id,orderId,unitId,star,content);
                    taskEvaluateAddList.add(jsonEvaluateAdd)
                    i += 1
                }
                mViewModel.addTaskEvaluate(taskEvaluateAddList)
            }
        }
    }

}